angular.module('Controller')
.controller('QgyAppointManageEditCtrl',($scope,urls,$localStorage,NgTableParams,commonUtil,cryptUtil,wfAlert,commonConstant,$http, $uibModalInstance, items)->
    $scope.data=items.data
    $scope.modalTitle='预约'

    loadApartment=()->
        $scope.apartmentList=[]
        $http.get('/apartment',params:{page:0,size:999,enabled:true})
        .success (resp)->
            if resp.code==0
                $scope.apartmentList=$scope.apartmentList.concat(resp.message.content)
                if $scope.apartmentList.length>0
                    $scope.data.buzProduct=$scope.apartmentList[0].id
                    loadingLayout()
            else
                console.log(resp.message)
        .error (err)->
            console.log(err)
    loadApartment()

    $scope.loadLayout = ()->
        $scope.data.buzItem=undefined
        $scope.layoutList=[{id:undefined,name:'未选择'}]
        loadingLayout()

    loadingLayout=()->
        if $scope.data.buzProduct==undefined
            return
        $scope.layoutList=[{id:undefined,name:'未选择'}]
        reqData={
            page: 0,size: 999,
            apartmentId:$scope.data.buzProduct
        }
        $http.get('/layout',params:reqData)
        .success (resp)->
            if resp.code==0
                $scope.layoutList=$scope.layoutList.concat(resp.message.content)

    $scope.confirm = ()->
        reqData={
            buzType:$scope.data.buzType,
            buzProduct:$scope.data.buzProduct,
            phone:$scope.data.userPhone,
            name:$scope.data.userName,
            content:$scope.data.content,
            startTime:moment($scope.data.startTime).format('YYYY-MM-DD HH:mm'),
            dealResult:$scope.data.dealResult,
        }
        $http.post("/appointment",reqData)
        .success((resp)->
            if resp.code==0
                wfAlert.toastInfo($scope.modalTitle+'成功')
                $uibModalInstance.close('done')
            else
                wfAlert.toastError(resp.message)
        )
    $scope.cancel =()->
        $uibModalInstance.dismiss('cancel')
)